package com.zzty.txj1.controller;


import com.zzty.txj1.entity.*;
import com.zzty.txj1.service.LookSelfExamService;
import com.zzty.txj1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/lookSelfExam")
public class LookSelfExamController {
    public static List<String>names=new ArrayList<>();
    public static List<String>fileNames=new ArrayList<>();
    @Autowired
    LookSelfExamService lookSelfExamService;
    @Autowired
    UserService userService;
    @RequestMapping("/commentHomework")
    public List<Users>getUsers(SelfExam selfExam, HttpServletRequest request){
        List<Users>users1=new ArrayList<>();
        User user1=(User)request.getSession().getAttribute("user");
        selfExam.setClassId(user1.getClazzid());
        System.out.println("selfExam"+selfExam.toString());
        Integer id=lookSelfExamService.getId(selfExam);
        System.out.println("id"+id);
        List<Integer>getUserIds=lookSelfExamService.getUserIds(id);
        System.out.println("selfExam"+getUserIds);
        for(int i=0;i<getUserIds.size();i++){
            User user=userService.getUserById(getUserIds.get(i));
            System.out.println("getUserIds.get(i)"+getUserIds.get(i));
            System.out.println("getUserIds.get(i)"+user.toString());
            String userName1=user.getName();
            SelfExamTransaction selfExamTransaction=new SelfExamTransaction();
            selfExamTransaction.setUserId(getUserIds.get(i));
            selfExamTransaction.setSelfExamId(id);
            SelfExamTransaction selfExamTransaction1=lookSelfExamService.getinfo(selfExamTransaction);
            Users users=new Users();
            String userNames=userName1;
            Integer userId=getUserIds.get(i);
            Integer score=selfExamTransaction1.getScore();
            String spendTime=selfExamTransaction1.getSpendTime();
            String comment=selfExamTransaction1.getComment();
            users.setUserId(userId);
            users.setScore(score);
            users.setUserName(userNames);
            users.setSpendTime(spendTime);
            users.setComment(comment);
            users1.add(users);
        }
        return users1;
    }


    @RequestMapping("/searchHomework")
    public Comment1 getFileName(Integer userId, String date,HttpServletRequest request){
        System.out.println("groupID"+userId+"date"+date);
        SelfExam selfExam=new SelfExam();
        selfExam.setDate(date);
        User user=(User)request.getSession().getAttribute("user");
        selfExam.setClassId(user.getClazzid());
        Integer id=lookSelfExamService.getId(selfExam);
        SelfExamTransaction selfExamTransaction=new SelfExamTransaction();
        selfExamTransaction.setSelfExamId(id);
        selfExamTransaction.setUserId(userId);
        SelfExamTransaction selfExamTransaction1=new SelfExamTransaction();
        selfExamTransaction1=lookSelfExamService.getinfo(selfExamTransaction);
        String answerUrl=selfExamTransaction1.getAnswerUrl();
        String comment1=selfExamTransaction1.getComment();
        Integer score=selfExamTransaction1.getScore() ;
        File file = new File(answerUrl);
        names.clear();
        List<String>name=finDiGui(file);
        System.out.println("answer1111"+names);
        Comment1 comment=new Comment1();
        comment.setNames(names);
        comment.setComment(comment1);
        comment.setScore(score);
        System.out.println("getnames"+comment.getNames());
        System.out.println("getnames"+comment.getComment());
        return comment;
    }

    @RequestMapping(value = "/fileNames" )
    public  Map<String,StringBuffer> getFileNames( String name){
        String str="";
        StringBuffer sb=new StringBuffer();
        String result="";
        Map<String,StringBuffer>map=new HashMap<>();
        System.out.println("name"+name);
        System.out.println("fileNames"+fileNames);
        for(int i=0;i<names.size();i++){
            if(name.equals(names.get(i))){
                result=fileNames.get(i);
            }
        }
        try {
            BufferedReader in = new BufferedReader(new FileReader(result));
            while ((str = in.readLine()) != null) {
                sb.append(str);
                sb.append("\n");
        }
        } catch (IOException e) {
        }
        map.put("result",sb);
        return map;
    }

    @RequestMapping(value = "/uploadResult" ,method = RequestMethod.POST)
    public  Integer getFileNames(String date,String comment,Integer score,Integer userId,HttpServletRequest request){
        System.out.println("date"+date+"comment"+comment+"score"+score+"groupID"+userId);
        SelfExam selfExam=new SelfExam();
        User user=(User)request.getSession().getAttribute("user");
        selfExam.setClassId(user.getClazzid());
        selfExam.setDate(date);
        Integer selfExamId=lookSelfExamService.getId(selfExam);
        SelfExamTransaction selfExamTransaction=new SelfExamTransaction();
        selfExamTransaction.setUserId(userId);
        selfExamTransaction.setSelfExamId(selfExamId);
        selfExamTransaction.setScore(score);
        selfExamTransaction.setComment(comment);
        Integer count=lookSelfExamService.updateSelfExamTransaction(selfExamTransaction);
        return count;
    }


    @RequestMapping(value = "/setWinnerId" ,method = RequestMethod.POST)
    public  Integer setWinnerId(Integer winnerId,String date,HttpServletRequest request){
        User user=new User();
        SelfExam selfExam=new SelfExam();
        User user1=(User)request.getSession().getAttribute("user");

        selfExam.setClassId(user1.getClazzid());
        selfExam.setDate(date);
        Integer selfExamId=lookSelfExamService.getId(selfExam);
        user=userService.getUserById(winnerId);
        String userName1=user.getName();
        Integer id1=userService.getUserByName(userName1).getId();
        SelfExam selfExam1=new SelfExam();
        selfExam1.setId(selfExamId);
        selfExam1.setWinnerId(id1);
        Integer count=lookSelfExamService.updateSelfExam(selfExam1);
        return count;
    }



    public List<String>  finDiGui(File file) {
        // 判断是否为文件夹
        if (file.isDirectory()) {
            File[] listFiles = file.listFiles();
            // 执行操作
            for (File f : listFiles) {
                finDiGui(f);
            }
        }
        if (file.getName().endsWith(".txt")||file.getName().endsWith(".java")||file.getName().endsWith(".xml")) {
            fileNames.add(file.getAbsolutePath());
            String[] fileName=file.getName().split("\\\\");
            for(String name:fileName){
                names.add(name);
            }
//            System.out.println(file);
        }
        return names;
    }
}
